<?php

namespace App\Console\Commands\Third\Xiaoke;

use App\Models\Third\Xiaoke\ClueChangeOwnerRecordModel;
use App\Models\Third\Xiaoke\ClueModel;
use App\Models\Third\Xiaoke\ClueToCustomerModel;
use App\Models\Third\Xiaoke\CustomerStageDataModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class FixHistoryDataCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'xk:fixHistoryRecords';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '销氪：修正历史数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // fix 销氪线索数据
        $this->getOutput()->info("fix 销氪线索数据");
        $this->fixXkClue();

        // fix 销氪线索转客户数据
        $this->getOutput()->info("fix 销氪线索转客户数据");
        $this->fixXkClueToCustomer();

        // fix 销氪线索变更所属人
        $this->getOutput()->info("fix 销氪线索变更所属人");
        $this->fixXkClueChangeOwnerRecord();

        // fix 销氪客户阶段数据
        $this->getOutput()->info("fix 销氪客户阶段数据");
        $this->fixXkCustomerStageData();

        return 0;
    }

    /**
     *  fix 销氪线索数据
     */
    public function fixXkClue()
    {
        ClueModel::query()->chunkById(100, function (Collection $collection) {
            $collection->each(function (ClueModel $clueModel) {
                $msgBodyArr = jsonToArr($clueModel->originalMsgBody, []);
                ClueModel::query()->where("id", $clueModel->id)->update([
                    "getTime"           => $msgBodyArr["clue"][0]["c_huodeshijian"] ?? "",
                    "formatCreatedXkAt" => formatDate($clueModel->createdXkAt),
                    "formatUpdatedXkAt" => formatDate($clueModel->updatedXkAt),
                    "formatCreatedAt"   => formatDate($clueModel->createdAt->timestamp),
                    "formatUpdatedAt"   => formatDate($clueModel->updatedAt->timestamp, formatDate(time())),
                ]);
            });
        });
    }

    /**
     * fix 销氪线索转客户数据
     */
    public function fixXkClueToCustomer()
    {
        ClueToCustomerModel::query()->chunkById(100, function (Collection $collection) {
            $collection->each(function (ClueToCustomerModel $clueToCustomerModel) {
                ClueToCustomerModel::query()->where("id", $clueToCustomerModel->id)->update([
                    "formatBuildTime"   => formatDate(substr($clueToCustomerModel->buildTime, 0, 10)),
                    "formatCreatedXkAt" => formatDate($clueToCustomerModel->createdXkAt),
                    "formatUpdatedXkAt" => formatDate($clueToCustomerModel->updatedXkAt),
                    "formatCreatedAt"   => formatDate($clueToCustomerModel->createdAt->timestamp),
                    "formatUpdatedAt"   => formatDate($clueToCustomerModel->updatedAt->timestamp, formatDate(time())),
                ]);
            });
        });
    }

    /**
     * fix 销氪线索变更所属人
     */
    public function fixXkClueChangeOwnerRecord()
    {
        ClueChangeOwnerRecordModel::query()->chunkById(100, function (Collection $collection) {
            $collection->each(function (ClueChangeOwnerRecordModel $clueChangeOwnerRecordModel) {
                ClueChangeOwnerRecordModel::query()->where("id", $clueChangeOwnerRecordModel->id)->update([
                    "formatCreatedAt" => formatDate($clueChangeOwnerRecordModel->createdAt->timestamp),
                    "formatUpdatedAt" => formatDate(
                        $clueChangeOwnerRecordModel->updatedAt->timestamp,
                        formatDate(time())
                    ),
                ]);
            });
        });
    }

    public function fixXkCustomerStageData()
    {
        CustomerStageDataModel::query()->chunkById(100, function (Collection $collection) {
            $collection->each(function (CustomerStageDataModel $customerStageDataModel) {
                $msgBodyArr = jsonToArr($customerStageDataModel->originalMsgBody, []);
                CustomerStageDataModel::query()->where("id", $customerStageDataModel->id)->update([
                    "formatBuildTime"   => formatDate(substr($customerStageDataModel->buildTime, 0, 10)),
                    "getTime"           =>
                        $customerStageDataModel->event == "edit" ?
                            ($msgBodyArr["fieldKeyValue"]["c_huodeshijian"][0] ?? "")
                            :
                            ($msgBodyArr["customer"]["c_huodeshijian"] ?? ""),
                    "formatCreatedXkAt" => formatDate($customerStageDataModel->createdXkAt),
                    "formatUpdatedXkAt" => formatDate($customerStageDataModel->updatedXkAt),
                    "formatCreatedAt"   => formatDate($customerStageDataModel->createdAt->timestamp),
                    "formatUpdatedAt"   => formatDate(
                        $customerStageDataModel->updatedAt->timestamp,
                        formatDate(time())
                    ),
                ]);
            });
        });
    }
}
